<template>
    <div>
        <h2>欢迎</h2>
        <div class="input-div two" id="codeBox">
            <div class="i">
                <i class="fas fa-lock"></i>
            </div>
            <div>
                <h5>验证码</h5>
                <input class="input" type="password" ref="code">
            </div>
        </div>
        <router-link to="/loginView/sendVerificationCode/">重新发送？</router-link>
        <input class="btn" type="submit" value="验 证" @click="checkVerificationCode">

        <van-action-sheet
            v-model="actionSheetShow"
            description="选择一个进行登录"
            :close-on-click-action="true"
            :close-on-popstate="true"
            :close-on-click-overlay="false"
        >
            <div v-for="userInfo in userInfoList" :key="userInfo['username']"
                 @click="selectUserLogin(userInfo['token'], false)">
                <van-icon :name="require('@/assets/images/loginView/wardUser.png')"/>
                <span>{{ userInfo['username'] }}</span>
            </div>
            <div @click="selectUserLogin(null, true)">
                <van-icon name="add"/>
                <span>注册用户并将其添加为我的监护对象</span>
            </div>
        </van-action-sheet>
    </div>
</template>

<script>
export default {
    name: "CheckVerificationCodeView",

    data() {
        return {
            actionSheetShow: false,
            userInfoList: [],
        }
    },

    methods: {
        init() {
            this.$(() => {
                const inputs = document.querySelectorAll('.input');

                function focusFunc() {
                    let parent = this.parentNode.parentNode;
                    parent.classList.add('focus');
                }

                function blurFunc() {
                    let parent = this.parentNode.parentNode;
                    if (!this.value) {
                        parent.classList.remove('focus');
                    }
                }

                inputs.forEach(input => {
                    input.addEventListener('focus', focusFunc);
                    input.addEventListener('blur', blurFunc);
                });
            })

        },

        checkVerificationCode() {
            let verificationCode = this.$refs.code.value

            if (!verificationCode) return this.$message({
                message: '验证码不能为空',
                type: 'warning'
            })

            // 登录
            let selectLoginType = this.$store.state.loginModule.selectLoginType
            this.request.post("/user/checkVerificationCode/", {
                selectLoginType: selectLoginType,
                verificationCode: verificationCode,
                emailVerificationRecordId: this.$store.state.loginModule.emailVerificationRecordId,
            }).then((response) => {
                const status = response["code"] === 200

                if (status) {
                    // 回写数据
                    this.userInfoList = response["data"]["userInfoList"]

                    // 如果是通过自己的邮箱登录，直接选择第一个用户进行登录
                    if (selectLoginType === "loginBySelfEmail") {
                        const userInfo = this.userInfoList[0]
                        this.selectUserLogin(userInfo['token'], userInfo['isNewWardUser'])
                    }
                    // 如果是通过监护人的邮箱登录，让用户自行选择登录
                    else if (selectLoginType === "loginByGuardianEmail") {
                        // 显示用户登录选择框
                        this.actionSheetShow = true
                    }
                }
            })
        },

        selectUserLogin(token, createNewWardUser) {
            this.request.post("/user/selectUserLogin/", {
                token: token,
                createNewWardUser: createNewWardUser,
                selectLoginType: this.$store.state.loginModule.selectLoginType,
                emailVerificationRecordId: this.$store.state.loginModule.emailVerificationRecordId
            }).then((response) => {
                const status = response["code"] === 200

                if (status) {
                    // 隐藏用户登录选择框
                    this.actionSheetShow = false
                    setTimeout(() => {
                        this.$router.push("/userView/")
                    }, 500)
                }
            })
        }
    },

    created() {
        this.init()
    }
}
</script>


<style lang="less" scoped>
.van-action-sheet__content > div {
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: center;

    .van-icon {
        width: 30px;
        height: 30px;
        margin-right: 5px;
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 1.5rem;
    }
}

* {
    margin: 0;
    box-sizing: border-box;
}

.input-div {
    position: relative;
    display: grid;
    grid-template-columns: 7% 93%;
    margin: 25px 0;
    padding: 5px;
    border-bottom: 2px solid #037971;
}

.input-div.focus .i i {
    color: #38d39f;
}

.input-div.focus div h5 {
    top: -5px;
    font-size: 15px;
    color: #38d39f;
}

.input-div:after, .input-div:before {
    content: '';
    position: absolute;
    bottom: -2px;
    width: 0;
    height: 2px;
    background-color: #38d39f;
    transition: .3s;
}

.input-div:after {
    right: 50%;
}

.input-div:before {
    left: 50%;
}

.input-div.two {
    margin-bottom: 4px;
}

.i {
    display: flex;
    justify-content: center;
    align-items: center;
}

.i i {
    color: #037971;
    transition: .3s;
}

.input-div > div {
    position: relative;
    height: 45px;
}

.input-div > div h5 {
    position: absolute;
    left: 10px;
    top: 50%;
    transform: translateY(-50%);
    color: #2b817b;
    font-size: 18px;
    transition: .3s;
}

.input {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    border: none;
    outline: none;
    background: none;
    padding: 0.5rem 0.7rem;
    font-size: 1.2rem;
    font-family: 'Poppins', sans-serif;
    color: #555;
}

a {
    display: block;
    text-align: right;
    text-decoration: none;
    color: #999;
    font-size: 0.9rem;
    transition: .3s;
}

a:hover {
    color: #037971;
}

.btn {
    display: block;
    width: 100%;
    height: 50px;
    border-radius: 25px;
    margin: 1rem 0;
    font-size: 1.2rem;
    outline: none;
    border: none;
    background-image: linear-gradient(to right, #32be8f, #38d39f, #32be8f);
    cursor: pointer;
    color: #fff;
    text-transform: uppercase;
    font-family: 'Poppins', sans-serif;
    background-size: 200%;
    transition: .5s;
}

.btn:hover {
    background-position: right;
}

h2 {
    font-size: 2.9rem;
    text-transform: uppercase;
    margin: 8px 0 15px;
    color: #037971;
}

@media screen and (max-width: 1000px) {
    h2 {
        font-size: 2.4rem;
        margin: 8px 0 15px;
    }
}
</style>
